package com.igroupes.ovineherd.model.attribute.swirch;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.SwitchStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 布尔
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class SwitchModelData extends BasicAttribute {

    private static Integer LENGTH = 1;
    public SwitchModelData() {
        super();
    }

    public Boolean verifyField(SwitchModelData json) {
         super.verifyField(json);
         return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        SwitchModelData switchModelData = (SwitchModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setType(MySqlTypeKey.BIGINT.getCode());
        fieldQuery.setLength(LENGTH);
        fieldQuery.setPrecision(0);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        return super.getAttribute(modelDataJsonAttributeQuery);
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);

        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        if(!NumberUtil.isNumber(value.toString())){
            throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
        }

        Integer data = Convert.toInt(value);
        if(!data.equals(BooleanKey.TRUE.getCode()) && !data.equals(BooleanKey.FALSE.getCode())){
            throw new ModelException(String.format("%s值范围错误, 只能为0,1",modelAttributeEntity.getName()));
        }
        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(value);
        fieldApiResponse.setIsStr(false);
        fieldApiResponse.setType(MySqlTypeKey.BIGINT.getCode());
        fieldApiResponses.add(fieldApiResponse);
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        return new JSONObject(new SwitchStyle(
                dataModelAttributeEntity.getName(), BooleanKey.TRUE.getCode(),
                false
        ), false);
    }
    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.NUMBER.getCode(), true, true, true);
    }
}
